﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddOrderInfo.aspx.cs" Inherits="ZX.WebApp.Crm.Sale.AddOrderInfo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/container/assets/skins/sam/container.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/button/assets/skins/sam/button.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/layout/assets/skins/sam/layout.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/resize/assets/skins/sam/resize.css" />
    <link rel="stylesheet" type="text/css" href="../../scripts/yui_2.9.0/build/calendar/assets/skins/sam/calendar.css" />
    <link rel="stylesheet" type="text/css" href="../../Scripts/yui_2.9.0/build/datatable/assets/skins/sam/datatable.css" />
    <script src="../../Scripts/jquery-1.6.min.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.json-2.2.js" type="text/javascript"></script>
    <script src="../../Scripts/Jquery.form.js" type="text/javascript"></script>
    <script src="../../Scripts/CombinButton.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/calendar/calendar-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/element/element-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/datasource/datasource-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/json/json-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/connection/connection-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/get/get-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/dragdrop/dragdrop-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/datatable/datatable-min.js"></script>
    <script type="text/javascript" src="../../Scripts/yui_2.9.0/build/paginator/paginator-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/layout/layout-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/button/button-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/container/container-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/resize/resize-min.js"></script>
    <script type="text/javascript" src="../../scripts/yui_2.9.0/build/event-delegate/event-delegate-min.js"></script>
    <link href="../../Styles/extendTable.css" rel="stylesheet" type="text/css" />
    <link href="../../Styles/base.css" rel="stylesheet" type="text/css" />
</head>
<body class="yui-skin-sam">
    <form id="form1" runat="server">
    <div id="con_two_1">
        <button type="button" id="btnNew1" name="btnNew1">
            新增</button>
        <button type="button" id="btnDelete1" name="btnDelete1">
            删除</button>
        <div id="con_1"></div>
    </div>
    <div id="dialog1" class="yui-pe-content">
        <div class="hd">
            信息选择</div>
        <div class="bd">
            <iframe id="EditUIWindow1" frameborder="0" src="" style="width: 100%; height: 100%;
                margin: 0px; padding: 0px;"></iframe>
        </div>
    </div>
    </form>
</body>
<script type="text/javascript">
    var myDataSource1, mDataTable1, condition = "", json = "";
    var strListPerson = [<%=ListPerson %>];
    var instance = [{ "NameList": "", "CustomerMrgList": ""}];
    var ChooseInfo = function () {
        var list = mDataTable1.getRecordSet()._records;
        var elist = [];
        for (var i = 0; i < list.length; i++) {
            if (list[i]._oData.UserId == "" || list[i]._oData.UserId == null) {
                alert("第" + (i + 1) + "行 姓名不能为空!");
                parent._DialogState = "0";
                return;
            }
            if (list[i]._oData.CommisionScale == "" || list[i]._oData.CommisionScale == null) {
                alert("第" + (i + 1) + "行 比例不能为空!");
                parent._DialogState = "0";
                return;
            }
            elist.push(list[i]._oData);
        }
        var TotalScale=0;
        for (var j = 0; j < elist.length; j++) {
            TotalScale += parseFloat(elist[j].CommisionScale);
            instance[0].CustomerMrgList += "@" + elist[j].UserId + "︾" + elist[j].CommisionScale + "︾" + elist[j].Explain + "︾";
            instance[0].NameList += elist[j].UserName.replace(/(^\s*)|(\s*$)/g, '') +"("+elist[j].CommisionScale+"%),";
        }
        if(TotalScale>100){
            alert("总比例之和不能超过100!");
            instance[0].CustomerMrgList="";
            instance[0].NameList="";
            parent._DialogState = "0";
            return;
        }else if(TotalScale<100) {
            alert("总比例之和必须等于100!");
            instance[0].CustomerMrgList="";
            instance[0].NameList="";
            parent._DialogState = "0";
            return;
        }
        parent._DialogState = "";
        parent._NameList = instance[0].NameList;
        parent._CustomerMrgList = instance[0].CustomerMrgList;
    }


    YAHOO.example.init = function () {
        function onButtonClick(p_oEvent) {
            switch (this.get("id")) {
                case "btnNew1":
                    handleAdd();
                    break;
                case "btnDelete1":
                    handleDelete();
                    break;
                default:
                    break;
            }
        }
        YAHOO.util.Event.onContentReady("con_two_1", function () {
            btnNews1 = new YAHOO.widget.Button("btnNew1");
            btnNews1.on("click", onButtonClick);
            btnDeletes1 = new YAHOO.widget.Button("btnDelete1");
            btnDeletes1.on("click", onButtonClick);
        });
    } ();

    var id="";
    var ChosePerson=function(idName){
        var entityName = "V_UserEmp";
        var condition = " isOutSite=0 and IsExpire=0";
        var src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&kid=UserID&kname=Name&entity=" + entityName + "&condition=" + decodeURI(condition) + "";
        id = idName;
        ShowDialog(getSelected, id, src);
    }
    var format1 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='UserName' style='width:70%; background: #e7eaf4;'>" +
                            "<button id='btnUserName' name='Z' type='button' onclick='ChosePerson(this.id);'>选择</button>";
    }
    /*var format1 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" onchange='ChangeEvent1(this);' name='UserName' style='width:95%;'>";
    }*/
    var format2 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" onchange='ChangeEvent1(this);' name='CommisionScale' style='width:95%;'>";
    }
    var format3 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" onchange='ChangeEvent1(this);' name='Explain' maxlength='100' style='width:99%;'>";
    }
    YAHOO.util.Event.addListener(window, "load", function () {
        BindData();
        hideCol();
    });
    var BindData = function () {
        YAHOO.example.InlineCellEditing = function () {
            var myColumnDefs1 = [
            { key: "ID", label: "ID", resizeable: true },
            { key: "UserId", label: "UserId", resizeable: true },
            { key: "UserName", label: "姓名", resizeable: true, formatter: format1 },
            { key: "CommisionScale", label: "比例(%)", resizeable: true, width: 150, formatter: format2,
                editor: new YAHOO.widget.TextboxCellEditor({ validator: YAHOO.widget.DataTable.validateNumber })
            },
            { key: "Explain", label: "备注", resizeable: true, formatter: format3 }
            ];
            myDataSource1 = new YAHOO.util.DataSource([{ ID: "", UserId: "", UserName: "", CommisionScale: "", Explain: ""}]);
            myDataSource1.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
            myDataSource1.responseSchema = {
                fields: ["ID", "UserId", "UserName", "CommisionScale", "Explain"]
            };
            var myDataTable1 = new YAHOO.widget.ScrollingDataTable("con_1",
                myColumnDefs1, myDataSource1, { width: "99%", height: "100%", selectionMode: "single" });
            mDataTable1 = myDataTable1;
            var i1 = 1,
            bReverseSorted1 = false;
            var trackReverseSorts1 = function (oArg) {
                bReverseSorted1 = (oArg.dir === YAHOO.widget.DataTable.CLASS_DESC);
            };
            var onEventSaveCellEditor = function (oArgs) {
                setValue1(oArgs.editor._oColumn.key, oArgs.newData);
            }
            myDataTable1.subscribe("rowMouseoverEvent", myDataTable1.onEventHighlightRow);
            myDataTable1.subscribe("rowMouseoutEvent", myDataTable1.onEventUnhighlightRow);
            myDataTable1.subscribe("rowClickEvent", myDataTable1.onEventSelectRow);
            myDataTable1.subscribe("cellClickEvent", myDataTable1.onEventShowCellEditor);
            myDataTable1.subscribe("editorSaveEvent", onEventSaveCellEditor);
            myDataTable1.subscribe("checkboxClickEvent", function (oArgs) {
                var elCheckbox = oArgs.target;
                var oRecord = this.getRecord(elCheckbox);
                oRecord.setData("iSun_Selected", elCheckbox.checked);
            });
            GetFormValue1();
            return {
                ds: myDataSource1,
                dt: myDataTable1
            };
        } ();
    }
    //隐藏/栏位
    function hideCol() {
        mDataTable1.hideColumn(parseInt(0)); mDataTable1.refreshView();
        mDataTable1.hideColumn(parseInt(1)); mDataTable1.refreshView();
    }


    YAHOO.namespace("example.container");
    YAHOO.util.Event.onDOMReady(function () {
        var handleSubmit = function () {
            //debugger
            var editwindow = document.getElementById("EditUIWindow1");
            if (editwindow) {
                if (_id == "btnUserName") {
                    YAHOO.example.container.dialog1.hide();
                    setValue1("UserName", editwindow.contentWindow.names+"&"+editwindow.contentWindow.ids);
                }
            }
            YAHOO.example.container.dialog1.hide();
        };
        var handleCancel = function () {
            this.cancel();
        };
        var handleSuccess = function (o) {
            var response = o.responseText;
            response = response.split("<!")[0];
            document.getElementById("resp").innerHTML = response;
        };
        var handleFailure = function (o) {
            alert("Submission failed: " + o.status);
        };
        YAHOO.example.container.dialog1 = new YAHOO.widget.Dialog("dialog1",
					                { width: "600px",
					                    height: "350px",
					                    fixedcenter: true,
					                    visible: false,
					                    draggable: true,
					                    close: true,
					                    modal: true,
					                    autofillheight: "body",
					                    constraintoviewport: true,
					                    buttons: [{ text: "确定", handler: handleSubmit, isDefault: true },
								                { text: "取消", handler: handleCancel}]
					                });
        YAHOO.example.container.dialog1.callback = { success: handleSuccess, failure: handleFailure };
        YAHOO.example.container.dialog1.render();
        //拖动大小dialog1
        var resize1 = new YAHOO.util.Resize("dialog1", {
            handles: ["br"],
            autoRatio: false,
            minWidth: 450,
            minHeight: 300,
            status: false
        });
        resize1.on("startResize", function (args) {
            if (this.cfg.getProperty("constraintoviewport")) {
                var D = YAHOO.util.Dom;
                var clientRegion = D.getClientRegion();
                var elRegion = D.getRegion(this.element);
                resize1.set("maxWidth", clientRegion.right - elRegion.left - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
                resize1.set("maxHeight", clientRegion.bottom - elRegion.top - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
            } else {
                resize1.set("maxWidth", null);
                resize1.set("maxHeight", null);
            }

        }, YAHOO.example.container.dialog1, true);
        resize1.on("resize", function (args) {
            var panelHeight = args.height;
            this.cfg.setProperty("height", panelHeight + "px");
        }, YAHOO.example.container.dialog1, true);
        //拖动大小dialog2
        var resize2 = new YAHOO.util.Resize("dialog2", {
            handles: ["br"],
            autoRatio: false,
            minWidth: 300,
            minHeight: 250,
            status: false
        });
    });
    //客户选择对应的方法和ID
    var _fn, _id;
    function ShowDialog(fn, id, src) {
        _fn = fn;
        _id = id;
        document.getElementById("EditUIWindow1").src = src;
        YAHOO.example.container.dialog1.show();
    }



    //值改变事件
    var ChangeEvent1 = function (oArgs) {
        var f = oArgs.name;
        var v = oArgs.value;
        setValue1(f, v);
    }
    //f=字段名 v=值
    var record;
    var setValue1 = function (f, v) {
        var recordID = mDataTable1.getSelectedRows()[0];
        if (recordID != null && recordID.length != 1) {
            record = mDataTable1.getRecord(recordID);
            record._oData[f] = v;
            if (f == "UserName") {
                //CheckUserIdByUserName(v.replace(/(^\s*)|(\s*$)/g, ''));
                var vTemp=v.split('&');
                var vTempName=vTemp[0].toString();
                var vname=vTempName.substring(0,vTempName.length-1);
                var vTempId=vTemp[1].toString();
                var vid=vTempId.substring(0,vTempId.length-1);
                $($($(".yui-dt-selected")[0]).find("input[name=UserName]")[0]).val(vname);
                record._oData["UserName"] = vname;
                $($($(".yui-dt-selected")[0]).find("input[name=UserId]")[0]).val(vid);
                record._oData["UserId"] = vid;
            } else if(f=="CommisionScale"){
                if (isNaN(v)) {
                    alert("比例必须输入数字!");
                    $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=CommisionScale]")[0]).val("");
                    record._oData["CommisionScale"] = "";
                }else{
                    if(v>100){
                        alert("比例不能超过100%!");
                        $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=CommisionScale]")[0]).val("");
                        record._oData["CommisionScale"] = "";
                    }
                }
            }
        } else {
            alert("请先选中当前行!");
        }
    }
    var setClear = function () {
        $($(event.srcElement.parentNode.parentNode.parentNode).find("input[name=UserName]")[0]).val("");
        record._oData["UserName"] = "";
        record._oData["UserId"] = "";
    }
    //根据输入的用户名判断用户是否存在
    var CheckUserIdByUserName = function (UserName) {
        var SaveUrl = "../../Extend/AjaxData/CustomOperating.aspx?action=check&entity=DatUser&json=" + encodeURI(UserName);
        YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
            success: function (res) {
                try {
                    var str = res.responseText;
                    if (str == "1") {
                        alert("系统中不存在该用户，请重新输入!");
                        record._oData["UserName"] = "";
                        record._oData["UserId"] = "";
                        return;
                    } else if (str == "0") {
                        alert("数据操作异常!");
                        record._oData["UserName"] = "";
                        record._oData["UserId"] = "";
                        return;
                    }
                    else {
                        record._oData["UserId"] = "" + str + "";
                        return;
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    }
    //设置值1
    var GetFormValue1 = function () {
        for (var i = 0; i < strListPerson.length; i++) {
            SetFormValue1(strListPerson);
        }
    }
    var SetFormValue1 = function (obj) {
        var arr = eval(obj);
        myDataSource1.liveData = arr;
        mDataTable1.getDataSource().sendRequest(null, { success: mDataTable1.onDataReturnInitializeTable }, mDataTable1);
    }


    //新增行
    var handleAdd = function () {
        mDataTable1.set("sortedBy", null);
        index = mDataTable1.getRecordSet()._records.length;
        var entity = [{ ID: "", UserId: "", UserName: "", CommisionScale: "", Explain: ""}];
        mDataTable1.addRows(entity, index);
    }
    //产品删除行
    var handleDelete = function () {
        var recordID = mDataTable1.getSelectedRows()[0];
        if (recordID != null && recordID.length != 1) {
            mDataTable1.deleteRow(recordID);
        } else {
            alert("请选中一行后删除！");
        }
    }
</script>
</html>
